<template>
    <div class="clinicPayment">
        <!-- clinicPayment -->
        <header>
            <Row>
                <Col>
                    <!-- <div class="margin-up-down">
                        <span>开始日期</span>
                        <DatePicker type="date" v-model="getParams.startDate" style='width:120px;'></DatePicker>
                    </div> -->
                    <div class="margin-up-down">
                        <Input v-model.trim="getParams.medOrgOrd" placeholder = "输入订单号" />
                    </div>
                    <div class="margin-up-down">
                        <Input v-model.trim="getParams.psnName" placeholder = "姓名" />
                    </div>
                    <div class="margin-up-down">
                        <Input v-model.trim="getParams.certno" placeholder = "身份证号" />
                    </div>
                    <div class="margin-up-down">
                        <Input v-model.trim="getParams.setlId" placeholder = "结算编号" />
                    </div>
                    <div class="margin-up-down">
                        <Input v-model.trim="getParams.psnNo" placeholder = "个人编号" />
                    </div>
                    <DatePicker
                        type="daterange"
                        v-model="getParams.filterTime"
                        placement="bottom-end"
                        placeholder="请选择日期"
                        separator="至"
                        style="width: 200px"
                    ></DatePicker>
                    <div class="margin-up-down">
                        <Button type='success' @click='loadingCardRechargeList(1)'>查询</Button>
                    </div>
                </Col>
            </Row>
        </header>
        <Table stripe :row-class-name="rowClassName" :columns="columns1" :data="data1" style='margin:10px 0;'></Table>
        <div class="page">
           <Page :total="count" :current='pageNo' :page-size="pageSize" show-total show-elevator @on-change="pageChange"/>
        </div>
    </div>
</template>
<script>
import api from "@/api/commonApi";
import Utils from "@/utils";

import { DatePicker, TimePicker, Card, Progress } from 'view-design'
// 诊间交费
export default {
    components: {
        DatePicker,
        TimePicker,
        Card,
        Progress
    },
    data () {
        const that = this;
        return {

          // 头部参数
            getParams: {
                // 开始时间
                medOrgOrd: '',
                psnName: '',
                certno: '',
                setlId: '',
                psnNo: '',
                filterTime: '',

                // startTime: "00:00",
                // 结束时间
                // endDate: "",
                // endTime: "",
                // 查询条件
                searchKey:"",
                // 异常
                single : false,
            },
            // 列表数据
            columns1: [
                {
                    key: "id",
                    title: "id",
                    align: "center",
                    width:60
                },
                {
                    key: "medOrgOrd",
                    title: "平台订单号",
                    align: "center"
                },
                {
                    key: "psnName",
                    title: "姓名",
                    align: "center"
                },
                {
                    key: "certno",
                    title: "身份证号",
                    align: "center",
                    // render: this.findTypeName
                },
                {
                    key: "setlId",
                    title: "结算编号",
                    align: "center"
                },
                {
                    key: "psnNo",
                    title: "个人编号",
                    align: "center"
                },
                {
                    key: "traceTime",
                    title: "交易时间",
                    align: "center"
                }
                ,
              {
                    key: "_operate",
                    title: "设置",
                    align: "center",
                    render(h, params) {
                        const {
                            id,
                        } = params.row;
                        return h(
                            "span",
                            {
                                props: {
                                    type: "primary",
                                },
                                style: {
                                    color: "#5362e1",
                                    margin: "0px 6px",
                                },
                                on: {
                                    click() {
                                        that.jumpTo(
                                                "/index/finance/insuranceAccounts/detail_1",
                                                {
                                                    id,
                                                }
                                            );
                                    },
                                },
                            },
                             "详情"
                        );
                    },
                },
            ],
            data1: [],
            // 分页数据
            count: 0,
            pageNo: 1,
            pageSize: 10,
            // 弹窗状态
            clinicStatus: false,
            // 单个平台状态列表
            statusList: ['失败','交易成功','退费', '未获取到状态'],
            //支付渠道
            Channels:['微信','支付宝','线下支付','就诊卡号'],
            // 点击来源
            buttonStatus: 1,
            orderStatusList: [
                {
                    name: "支付成功",
                    id:'1'
                },
                {
                    name: "未支付",
                    id:'2'
                },
                {
                    name: "支付失败",
                    id:'3'
                },
                {
                    name: "支付取消",
                    id:'4'
                },
                {
                    name: "已退费",
                    id:'5'
                },
            ],
            // 订单对账状态
            manualParams: {
                // 平台状态
                PlatformStatus: '已支付',
                // 互联网平台状态
                interPlatformStatus: "",
                // HIS状态
                hisPlatformStatus: "",
                // 弹窗查询流水号
                modalSearchKey:"",
                // id
                transId:""
            },
            stautParams: ['未对账','对账成功','未获取到数据','金额不符','状态不符'],
            // // 自动对账状态
            autoLoad:false,
            // 进度条
            progressStart: 0,
            uuid :'',
            DZdisabled: false,
        }
    },
    created () {
        let breadList = [
            { path: "/index", title: "首页" },
            {
                path: "/index/operation/clinicPayment",
                title: "互联网医院"
            },
            {
                path: "/index/internetHospital/clinicPayment/list",
                title: "诊间缴费对账"
            }
        ];
        // this.getParams.startDate = this.GetDate(0);
        // this.getParams.endDate = this.GetDate(0);
        // let date = new Date();
        // this.getParams.endTime = date.getHours() + ':' + date.getMinutes()
        this.$emit("changeBreadList", breadList);
    },
    mounted () {
        this.loadingCardRechargeList(this.pageNo)
    },
    methods: {
        query () {
          let url = api.insuranceBillSelect;
          let param = {
            // startTime: this.getData(this.getParams.startDate)
            // hospitalId: sessionStorage.getItem("hospitalId")
            medOrgOrd : this.getParams.medOrgOrd,
          }
          this.$axios.post(url, param).then(resp => {
            this.data1 = resp.data.object;
          })
        },
        // 分页器改变
        pageChange (index) {
            this.pageNo = index;
            this.loadingCardRechargeList(index)
            // this.query(index)
        },
        // 自动对账确定按钮
        ok () {
            this.autoLoad = true
            this.autorecon();
        },
        // 自动对账
        autorecon () {
            this.DZdisabled = true
            let url = api.checkingCheck
            let params = {
                // pageNo: this.pageNo,
                // pageSize: this.pageSize,
                startTime: this.getData(this.getParams.startDate) + ' ' + this.getParams.startTime,
                endTime: this.getData(this.getParams.endDate) + ' ' + this.getParams.endTime,
                status: Number(this.getParams.single),
                businessSources: 22,
                // businessSources: 11,
            }
            this.$axios.post(url, params,{timeout: 1000 * 60 * 2}).then(res => {
                if (res.data.code == 1) {
                    let ret = res.data.object;
                    // this.uuid = ret.key
                    // this.progressbord()
                    this.DZdisabled = false
                    this.clinicStatus = false
                    this.$Message.success('对账成功')
                    this.loadingCardRechargeList(1)
                    console.log('自动对账',res);
                } else if (res.data.code == 2) {
                   this.$Message.info("正在对账,请勿重复对账!")
                } else {
                    this.$Message.error(res.data.message)
                    this.DZdisabled = false
                }
            })
        },
        // 请求进度条
        progressbord () {
            let url = api.speedofprogress;
            let params = {
                uuid: this.uuid
            }
            this.$axios.post(url, params).then(res => {
                if(res.data.success) {
                    let ret = res.data.object;
                    console.log("进度条", ret);
                    let err = Boolean(ret.ERROR_COUNT) ? Number(ret.ERROR_COUNT) : 0
                    let start = Number(ret.EXECUTE_START)
                    let count = Boolean(ret.EXECUTE_COUNT) ? Number(ret.EXECUTE_COUNT) : 0
                    let currentTotal = err + count
                    console.log('currentTotal: ', currentTotal);

                    // 进度条参数
                    let progressEnd = Number(ret.TOTAL_COUNT) || 0
                    console.log('progressEnd: ', progressEnd);
                    let progressStart = parseInt((currentTotal * 100) / progressEnd);
                    console.log('progressStart: ', progressStart);

                    this.progressStart = progressStart
                    if(start != 1) {
                        return ""
                    }
                    // 当已对账数量+异常数据 = 总数的时候停止递归然后调用取消对账接口
                    if(currentTotal == ret.TOTAL_COUNT) {
                        this.$axios.post(api.deleteredis, {
                            uuid: this.uuid
                        }).then(res => {
                            if (res.data.success) {
                                this.clinicStatus = false;
                                this.autoLoad = false;
                                this.progressStart = 100
                                this.loadingCardRechargeList(this.pageNo);
                                this.$Message.success("对账成功")
                            }
                        })
                        return ""
                        // 不等于总数时继续递归调用进度条接口
                    } else if(currentTotal != ret.TOTAL_COUNT) {
                        console.log("继续对账");
                        if (this.autoLoad) {
                            this.progressbord();
                        }
                    }
                }
            })
        },
        // 点击弹窗取消,初始化弹窗里的值
        cancel () {
            this.clinicStatus = false
            //支付状态
            this.manualParams.PlatformStatus = ''
            this.manualParams.interPlatformStatus = ''
            this.manualParams.hisPlatformStatus = ''
            this.manualParams.modalSearchKey = ''
        },
      jumpTo(path, query = {}) {
            this.$router.push({
                path,
                query: Object.assign(
                    {
                        filterObj: encodeURIComponent(
                            JSON.stringify(this.filterObj)
                        ),
                    },
                    query
                ),
            });
        },
        //更改平台状态
        changePlatform () {
            console.log('更改平台状态');
            let url = api.updatestatus
            let params = {}
            return ""
            this.$axios.post(url, params).then(res => {
                if (res.data.success) {
                    // this.$Message.info('由于对账信息较多,请等待5-10分钟重新查看!');
                } else {
                    this.$Message.error("更改平台状态失败")
                }
                this.clinicStatus = false
            })
        },
        // 查询账单流水号
        searchSerialNumber () {
            let url = api.querybyserialnumber;
            let params = {
                serialNumber: this.manualParams.modalSearchKey
            }
            console.log(params);
            // return ""
            this.$axios.post(url,params).then(res => {
                if(res.data.success) {
                    let ret = res.data.object;
                    this.cliniction(2,ret)
                    console.log(ret);
                }
            })
        },
        //更改His平台状态
        changeHisStatus () {
            console.log('更改His平台状态');
            let url = ''
            let params = {}
            return ""
            this.$axios.post(url, params).then(res => {
                if (res.data.success) {

                } else {
                    this.$Message.error("更改His平台状态失败")
                }
                this.clinicStatus = false
            })
        },
        // 加载页面列表数据
        loadingCardRechargeList (pageNo) {
            /*
                加载页面列表对账信息
            */
            this.pageNo = pageNo
            let url = api.insuranceBillSelect
            let medOrgOrd = this.getParams.medOrgOrd.trim();
            let psnNo = this.getParams.psnNo.trim();
            let setlId = this.getParams.setlId.trim();
            let certno = this.getParams.certno.trim();
            let psnName = this.getParams.psnName.trim();
            let query = this.getParams.searchKey.trim();
            let startTime = this.getParams.filterTime
                ? Utils.getNowDateBySpecialFormat(this.getParams.filterTime[0])
                : null;
            let endTime = this.getParams.filterTime
                ? Utils.getNowDateBySpecialFormat(this.getParams.filterTime[1])
                : null;
            let params = {
                pageNo: pageNo,
                pageSize: this.pageSize,
                psnNo: psnNo,
                setlId: setlId,
                certno: certno,
                psnName: psnName,
                startTime:startTime,
                endTime:endTime,
                // startTime: this.getData(this.getParams.startDate) + ' ' + this.getParams.startTime,

                // endTime: this.getData(this.getParams.endDate) + ' ' + this.getParams.endTime,

                // startTime: startTime ? startTime : '',
                // endTime: endTime ? endTime : '',
                // status: Number(this.getParams.single),
                // businessSources: 11,
                medOrgOrd:medOrgOrd
            }
            if (Boolean(this.getParams.searchKey)) {
                params.query = this.getParams.searchKey.trim();
            } else {
                params.query = ''
            }
            if(this.getParams.startDate === '' || this.getParams.startDate === null){
                delete params.startTime
            }
            if(this.getParams.endDate === '' || this.getParams.endDate === null){
                delete params.endTime
            }
            console.log('诊间交费参数', params);
            this.$axios.post(url,params).then(ress => {
                if(ress.data.success) {
                    let ret = ress.data.object;
                    this.count = ret.total
                    console.log(ret);
                    if(ret.records){
                        ret.records.forEach((item, index) => {
                            item.isum = this.addZeros(index)
                        })
                        this.data1 = ret.records
                    } else {
                        this.data1 = []
                    }
                } else {
                    this.$Message.info("请确认当前账号是否绑定机构")
                }
            })
        },
        // 显示异常数据
        rowClassName (row, index) {
            if(Number(row.checkingStatus) == 0) {
                return ''
            } else if (row.tradingChannels >= 0) {
                // console.log('row.tradingChannels', row.tradingChannels)
                if(row.tradingChannels == 3) {
                    // 当支付渠道为就诊卡时只使用 支付平台与His对账的状态区分显示样式
                    if(row.platformHisStatus != 1) {
                        return 'demo-error-row';
                    } else {
                        return 'demo-success-row'
                    }
                    // 支付渠道为其他状态时 使用三个对比状态进行区分显示样式
                } else if(row.platformHisStatus != 1 || row.thirdHisStatus != 1 || row.platformThirdStatus != 1) {
                    console.log('hhhhhhhhhhhhhh')
                    return 'demo-error-row';
                }
            } else if(Number(row.checkingStatus) == 1) {
                return 'demo-success-row'
            }
            return '';
        },
        // 转换日期格式
        getData(data){
            let datas = new Date(data)
            var curr_date = datas.getDate();
            var curr_month = datas.getMonth() + 1;
            var curr_year = datas.getFullYear();
            function add(num){
                if(Number(num)<10) {
                    return '0' + num
                }
                return num
            }
            return curr_year + "-" + add(curr_month)+'-' + add(curr_date)
        }
    }
};
</script>

<style lang="less" scoped>
.clinicPayment {
    width: calc(100%);
    padding: 10px 30px;
    margin: 0 auto;
    background: #fff;
    .margin-up-down {
        display: inline-block;
        margin-top: 10px;
    }
    .page{
        text-align: center;
        margin-top: 20px;
    }
}
</style>
<style>
.ivu-table .demo-error-row td{
    color: red;
}
.ivu-table .demo-error-row td a{
    color:red
}
.ivu-table .demo-success-row td{
    color: #2b85e4;
}
.ivu-table .demo-success-row td a{
    color:#2b85e4
}
</style>
